package com.yjkf.zh.admin.service.impl;

import com.yjkf.zh.admin.dao.SysLoginLogMapper;
import com.yjkf.zh.admin.model.SysLoginLog;
import com.yjkf.zh.admin.service.SysLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysLoginLogServiceImpl implements SysLoginLogService {

    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;

    @Transactional
    @Override
    public int writeLoginLog(String userName, String ip) {
        List<SysLoginLog> sysLoginLogs = sysLoginLogMapper.findByUserNameAndStatus(userName,ip);
        for(SysLoginLog sysLoginLog:sysLoginLogs){
            sysLoginLog.setStatus(SysLoginLog.STATUS_LOGIN);
            sysLoginLogMapper.updateByPrimaryKey(sysLoginLog);
        }
        SysLoginLog record = new SysLoginLog();
        record.setUserName(userName);
        record.setIp(ip);
        record.setStatus(SysLoginLog.STATUS_LOGOUT);
        sysLoginLogMapper.insertSelective(record);
        return 0;
    }
}
